<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
    <!-- table -->
    <!-- 特殊配件管理费率维护 - 特殊配件管理费率编辑 -->
    <FormPanel ref="formPanel1" :list="formList" :cols="1" :formType="'edit'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 1rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button  @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Vehicle @close="close" @save="save"></Vehicle>
    </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import { copyUpdateData } from '@/utils/index';
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
import { editSave , selectById,getModelList } from '@/api/claim/factory/SpePartManageHourMaintain';
import moment from "moment";
import { getAppId} from '@/assets/js/auth';
import {dictionary} from'@/utils/dictMixin';
export default {
  props: ['type', 'rowData'],
   mixins: [dictionary],
  components: {
    Vehicle
  },
  data() {
    return {
      loading: false,
      dialogTitle: '配件选择',
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
      newVar: {},
      ModelList:[]

    };
  },
  created() {
    // if(getAppId()=='beijing'){
    //   this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
    // }else{
    //   this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
    //   this.formList.find((x) => x.fieldName == 'powerType').initialValue = 60081002;
    //   this.formList.find((x) => x.fieldName == 'powerType').disabled=true;
    // }
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.loading = true;
      // debugger
      console.log(this.rowData.mrId)
      let newVar = await selectById({id: this.rowData.mrId});
       console.log(newVar.data);
       this.formList.forEach(x => {
        console.log(this.rowData);
        x.initialValue = newVar.data[x.fieldName];

      });
      this.newVar = newVar.data;
      this.loading = false;
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      debugger
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      // if(this.saveData.beginEffect){
      //   this.saveData.beginEffect = moment(this.saveData.beginEffect).format('YYYY-MM-DD HH:mm:ss');
      // }
      // if(this.saveData.endEffect){
      //   this.saveData.endEffect = moment(this.saveData.endEffect).format('YYYY-MM-DD HH:mm:ss');
      // }
      if (this.saveData !== null) {
        console.log(this.saveData);
        this.newVar = copyUpdateData(this.newVar,this.saveData);
        this.loading = true;
        let res = await editSave(this.newVar);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.formList.find(x => x.fieldName == 'supplierCode').initialValue = obj[0].supplierCode;
      this.formList.find(x => x.fieldName == 'supplierName').initialValue = obj[0].supplierName;
      this.visible = false;
    },
     //获取车型
    getCarData() {
      let arr = []
      getModelList().then(res => {
        if (res.data) {

          console.log(res.data)
            res.data.forEach(function (item, i) {
              if (item.modelCode) {
              arr.push({ text: item.modelName, value: item.modelCode});
              }
            })
            this.ModelList = arr;
        }
      }
      )
      return arr
    },
    createFormList() {
      return [
        // {
        //   type: 'INPUT',
        //   label: '配件代码',
        //   fieldName: 'partCode',
        //   placeholder: '请选择配件代码',
        //   readonly: true, // 只读
        //   rules: [{ required: true, message: '请选择配件代码', trigger: 'change' }],
        //   unitRender: p => {
        //     return (
        //       <div>
        //         <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
        //       </div>
        //     );
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '配件名称',
        //   fieldName: 'partName',
        //   rules: [{ required: true, message: '请输入配件名称...', trigger: 'change' }],
        //   readonly: true,// 只读
        //   disabled: true // 置会
        // },
        // {
        //   type: 'INPUT',
        //   label: '费率',
        //   fieldName: 'mngRate',
        //   placeholder: '请输入费率...',
        //   rules: [
        //     { required: true, message: '请输入费率...', trigger: 'blur' },
        //     {
        //       validator: (rule, value, callback) => {
        //         let reg = /^(\d+)(\.?)(\d{0,2})$/;
        //         if (!reg.test(Number(value))) {
        //           return callback(new Error('费率,最多2位小数...'));
        //         }
        //         callback();
        //       },
        //       trigger: 'blur',
        //     },
        //   ],
        //   unitRender: () => {
        //     return <span>(%)</span>;
        //   },
        // },
        // {
        //   type: 'INPUT',
        //   label: '配件最大单价',
        //   fieldName: 'maxPrice',
        //   placeholder: '请输入配件最大单价',
        //   // disabled: true,
        //   unitRender: (p) => {
        //     return <div>元</div>;
        //   },
        //   rules: [
        //     { required: true, message: '请输入配件最大单价', trigger: 'change' },
        //     {
        //       validator: (rule, value, callback) => {
        //         let reg = /^(\d+)(\.?)(\d{0,2})$/;
        //         if (value < 0) {
        //           return callback(new Error('配件最大单价,最小为0'));
        //         }
        //         if(!reg.test(Number(value))){
        //           return callback(new Error('配件最大单价,最多2位小数'));
        //         }
        //         callback();
        //       },
        //       trigger: 'change',
        //     }
        //   ],
        // },
        // {
        //   type: 'RANGE_DATE',
        //   label: '生效时间',
        //   fieldName: 'beginEffect|endEffect',
        //   style: { minWidth: '200px' },
        //   rules: [{ required: true, message: '请输入时间', trigger: 'change' }],
        //   valueFormat: 'yyyy-MM-dd'
        // },
        // {
        //   type: 'SELECT',
        //   label: '动力形式',
        //   fieldName: 'powerType',
        //   placeholder: '请选择动力形式...',
        //   initialValue: '',
        //   itemList: this.createDictList('6008'),
        //   filterable:true,
        //   rules: [{required: true, message: '请选择动力形式...', trigger: 'change'}]
        // },
         {
          type: 'INPUT',
          label: '配套厂编号:',
          fieldName: 'supplierCode',
          placeholder: '请输入配套厂编号...',
          readonly: true,// 只读
          disabled: true, // 置会
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '配套厂名称:',
          fieldName: 'supplierName',
          placeholder: '请输入配套厂名称...',
          readonly: true,// 只读
          disabled: true, // 置会
          initialValue: '',
        },

        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'vehicleType',
          initialValue: '',
          itemList:this.getCarData(),
          rules: [{required: true, message: '请输入车型...', trigger: 'change'}]
          },
        // {
        //   type: 'SELECT',
        //   label: '车型',
        //   fieldName: 'vehicleType',
        //   placeholder: '请选择车型...',
        //   initialValue: '',
        //   itemList:[],
        //   rules: [{required: true, message: '请选择车型...', trigger: 'change'}]
        //   },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          placeholder: '请选择状态...',
          initialValue: 55981001,
          itemList: [
            { text: '启用', value: 55981001 },
            { text: '禁用', value: 55981002 },
          ],
          filterable: true,
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }],
        },
           {
          type: 'INPUT',
          label: '配套厂备件管理费率:',
          fieldName: 'manageRate',
          placeholder: '请输入费率...',
          rules: [
            { required: true, message: '请输入费率...', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (!reg.test(Number(value))) {
                  return callback(new Error('费率,最多2位小数...'));
                }
                callback();
              },
              trigger: 'blur',
            },
          ],
          unitRender: () => {
            return <span>(%)</span>;
          },
        },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
